{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 生成训练、测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#保存数据\n",
    "import pickle\n",
    "\n",
    "import numpy as np\n",
    "import scipy.io as sio\n",
    "import scipy.sparse as ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-4-2860af8bc11a>, line 119)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-4-2860af8bc11a>\"\u001b[1;36m, line \u001b[1;32m119\u001b[0m\n\u001b[1;33m    print \"%s:%d (userId, eventId)=(%s, %s)\" % (fn, ln, userId, eventId)\u001b[0m\n\u001b[1;37m                                           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 将所有特征串联起来，构成FE_Train.csv\n",
    "#FE_Test.csv\n",
    "#为最后推荐系统做准备\n",
    "from __future__ import division\n",
    "\n",
    "class DataRewriter:\n",
    "  def __init__(self):\n",
    "    # 读入数据做初始化\n",
    "    self.userIndex = pickle.load(open(\"PE_userIndex.pkl\", 'rb'))\n",
    "    self.eventIndex = pickle.load(open(\"PE_eventIndex.pkl\", 'rb'))\n",
    "    \n",
    "    self.userEventScores = sio.mmread(\"PE_userEventScores\").todense()\n",
    "    \n",
    "    self.userSimMatrix = sio.mmread(\"US_userSimMatrix\").todense()\n",
    "    \n",
    "    self.eventPropSim = sio.mmread(\"EV_eventPropSim\").todense()\n",
    "    self.eventContSim = sio.mmread(\"EV_eventContSim\").todense()\n",
    "    \n",
    "    self.numFriends = sio.mmread(\"UF_numFriends\")\n",
    "    self.userFriends = sio.mmread(\"UF_userFriends\").todense()\n",
    "    \n",
    "    self.eventPopularity = sio.mmread(\"EA_eventPopularity\").todense()\n",
    "    \n",
    "  def userReco(self, userId, eventId):\n",
    "\n",
    "    #根据User-based协同过滤，得到event的推荐度\n",
    "    #基本的伪代码思路如下：\n",
    "    #for item i\n",
    "      #for every other user v that has a preference for i\n",
    "        #compute similarity s between u and v\n",
    "        #incorporate v's preference for i weighted by s into running aversge\n",
    "    #return top items ranked by weighted average\n",
    "\n",
    "    \n",
    "    #请自行补充eventId对userId推荐度\n",
    "    return 0\n",
    "\n",
    "  def eventReco(self, userId, eventId):\n",
    "\n",
    "    #根据基于物品的协同过滤，得到Event的推荐度\n",
    "    #基本的伪代码思路如下：\n",
    "    #for item i \n",
    "     # for every item j tht u has a preference for\n",
    "        #compute similarity s between i and j\n",
    "        #add u's preference for j weighted by s to a running average\n",
    "   # return top items, ranked by weighted average\n",
    " \n",
    "    pscore = 0\n",
    "    cscore = 0\n",
    "    \n",
    "    #请自行补充eventId对userId推荐度\n",
    "    return pscore, cscore\n",
    "\n",
    "    def ModelReco(self, userId, eventId):\n",
    "        #请自行补充基于模型的协同过滤\n",
    "        #SVD++/LFM\n",
    "        return 0\n",
    "\n",
    "\n",
    "  def userPop(self, userId):\n",
    "\n",
    "    #基于用户的朋友个数来推断用户的社交程度\n",
    "    #主要的考量是如果用户的朋友非常多，可能会更倾向于参加各种社交活动\n",
    "\n",
    "    if self.userIndex.has_key(userId):\n",
    "      i = self.userIndex[userId]\n",
    "      try:\n",
    "        return self.numFriends[0, i]\n",
    "      except IndexError:\n",
    "        return 0\n",
    "    else:\n",
    "      return 0\n",
    "\n",
    "  def friendInfluence(self, userId):\n",
    "\n",
    "    #朋友对用户的影响\n",
    "    #主要考虑用户所有的朋友中，有多少是非常喜欢参加各种社交活动/event的\n",
    "    #用户的朋友圈如果都积极参与各种event，可能会对当前用户有一定的影响\n",
    "\n",
    "    nusers = np.shape(self.userFriends)[1]\n",
    "    i = self.userIndex[userId]\n",
    "    return (self.userFriends[i, :].sum(axis=0) / nusers)[0,0]\n",
    "\n",
    "  def eventPop(self, eventId):\n",
    "\n",
    "    #本活动本身的热度\n",
    "    #主要是通过参与的人数来界定的\n",
    "\n",
    "    i = self.eventIndex[eventId]\n",
    "    return self.eventPopularity[i, 0]\n",
    "\n",
    "    \n",
    "  def rewriteData(self, start=1, train=True, header=True):\n",
    "\n",
    "    #把前面user-based协同过滤 和 item-based协同过滤，以及各种热度和影响度作为特征组合在一起\n",
    "    #生成新的训练数据，用于分类器分类使用\n",
    "\n",
    "    fn = \"train.csv\" if train else \"test.csv\"\n",
    "    fin = open(fn, 'rb')\n",
    "    fout = open(\"data_\" + fn, 'wb')\n",
    "    # write output header\n",
    "    if header:\n",
    "      ocolnames = [\"invited\", \"user_reco\", \"evt_p_reco\",\n",
    "        \"evt_c_reco\", \"user_pop\", \"frnd_infl\", \"evt_pop\"]\n",
    "      if train:\n",
    "        ocolnames.append(\"interested\")\n",
    "        ocolnames.append(\"not_interested\")\n",
    "      fout.write(\",\".join(ocolnames) + \"\\n\")\n",
    "    ln = 0\n",
    "    for line in fin:\n",
    "      ln += 1\n",
    "      if ln < start:\n",
    "        continue\n",
    "      cols = line.strip().split(\",\")\n",
    "      userId = cols[0]\n",
    "      eventId = cols[1]\n",
    "      invited = cols[2]\n",
    "      if ln%500 == 0:\n",
    "          print (\"%s:%d (userId, eventId)=(%s, %s)\" % (fn, ln, userId, eventId))\n",
    "      user_reco = self.userReco(userId, eventId)\n",
    "      evt_p_reco, evt_c_reco = self.eventReco(userId, eventId)\n",
    "      user_pop = self.userPop(userId)\n",
    "      frnd_infl = self.friendInfluence(userId)\n",
    "      evt_pop = self.eventPop(eventId)\n",
    "      ocols = [invited, user_reco, evt_p_reco,\n",
    "        evt_c_reco, user_pop, frnd_infl, evt_pop]\n",
    "      if train:\n",
    "        ocols.append(cols[4]) # interested\n",
    "        ocols.append(cols[5]) # not_interested\n",
    "      fout.write(\",\".join(map(lambda x: str(x), ocols)) + \"\\n\")\n",
    "    fin.close()\n",
    "    fout.close()\n",
    "\n",
    "  def rewriteTrainingSet(self):\n",
    "    self.rewriteData(True)\n",
    "\n",
    "  def rewriteTestSet(self):\n",
    "    self.rewriteData(False)\n",
    "\n",
    "\n",
    "dr = DataRewriter()\n",
    "print \"生成训练数据...\\n\"\n",
    "dr.rewriteData(train=True, start=2, header=True)\n",
    "\n",
    "print \"生成预测数据...\\n\"\n",
    "dr.rewriteData(train=False, start=2, header=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
